package cn.com.inossem.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.foxykeep.datadroid.requestmanager.Request;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.com.inossem.activity.R;
import cn.com.inossem.entity.StatusEnum;
import cn.com.inossem.requestmanager.WooRequestManager;
import cn.com.inossem.utils.Utils;

/**
 * 
 * The goal of this base class: 1. bind the Datadroid to use 2. bind the
 * lifecycle of fragmeng to Datadroid callback 3. process the loading and error
 * state
 * 
 */
public abstract class RequestFragment extends Fragment implements RequestBaseUi {
	protected static final String SAVED_STATE_REQUEST_LIST = "savedStateRequestList";
	protected WooRequestManager mRequestManager = null;
	protected ArrayList<Request> mRequestList = null;
	protected boolean mContextDestroyed = false;
	protected View mRootView = null;
	
	public ProgressDialog progressDialog;
	Unbinder unbinder;
	// protected StatusEnum statusEnum;
	public boolean mHasLoadedOnce = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mRootView = inflater.inflate(getContentViewId(), container, false);
		unbinder = ButterKnife.bind(this, mRootView);
		initAllMembers(savedInstanceState);
		return mRootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		if (Utils.isNetworkAvailable(getActivity())) {
			launchRequest(getInitialRequest());
			if (needShowLoadingIndicator()) {
				// onLoadingIndicatorShow(statusEnum);
			}
		} else {
			Toast.makeText(getActivity(), "未检查到可用的网络！", Toast.LENGTH_LONG).show();
			handleException(EXCEPTION_TYPE_NETWORK);
		}
	}

	@Override
	public void onDestroyView() {
		mContextDestroyed = true;
		super.onDestroyView();
		unbinder.unbind();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putParcelableArrayList(SAVED_STATE_REQUEST_LIST, mRequestList);
	}

	@Override
	public void initAllMembers(Bundle savedInstanceState) {
		mContextDestroyed = false;
		mRequestManager = WooRequestManager.from(getActivity());

		if (savedInstanceState != null) {
			mRequestList = savedInstanceState
					.getParcelableArrayList(SAVED_STATE_REQUEST_LIST);
		}
		if (mRequestList == null) {
			mRequestList = new ArrayList<Request>();
		}
	}

	@Override
	public void initLoader() {

	}

	@Override
	public Request getInitialRequest() {
		return null;
	}

	@Override
	public final void launchRequest(Request request) {
		if (request != null) {
			if (Utils.isNetworkAvailable(getActivity())) {
				mRequestManager.execute(request, this);
				mRequestList.add(request);

			} else {
				handleException(EXCEPTION_TYPE_NETWORK);
			}
		}
	}

	@Override
	public final void onRequestConnectionError(Request arg0, int arg1) {
		if (mRequestList.contains(arg0)) {
			mRequestList.remove(arg0);
		}

		if (arg1 == -1) {
			handleException(EXCEPTION_TYPE_NETWORK);
		} else {
			handleException(EXCEPTION_TYPE_SERVER_NO_RESPONSE);
		}
	}

	@Override
	public final void onRequestCustomError(Request arg0, Bundle arg1) {
		if (mRequestList.contains(arg0)) {
			mRequestList.remove(arg0);
		}

		handleException(EXCEPTION_TYPE_CUSTOM_BASE);
	}

	@Override
	public final void onRequestDataError(Request arg0) {
		if (mRequestList.contains(arg0)) {
			mRequestList.remove(arg0);
		}

		handleException(EXCEPTION_TYPE_DATA);
	}

	@Override
	public final void onRequestFinished(Request arg0, Bundle arg1) {
		if (mContextDestroyed) {
			return;
		}

		if (mRequestList.contains(arg0)) {
			onLoadingIndicatorHide();
			initLoader();
			onRequestSucess(arg0, arg1);
			mRequestList.remove(arg0);
		}
	}

	@Override
	public final void handleException(int exceptionType) {
		if (mContextDestroyed) {
			return;
		}
		onLoadingIndicatorHide();
		onRequestError(exceptionType);
	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {

	}

	@Override
	public void onRequestError(int exceptionType) {
		View errorIndicatorLayout = getErrorIndicatorLayout();
		TextView errorMsgTextView = getErrorMsgTextView();
		if (errorIndicatorLayout == null || errorMsgTextView == null) {
			return;
		}

		switch (exceptionType) {
		case EXCEPTION_TYPE_NETWORK:
			errorMsgTextView.setText(R.string.tips_network_connect_error);
			errorIndicatorLayout.setVisibility(View.VISIBLE);
			break;
		case EXCEPTION_TYPE_DATA:
			errorMsgTextView.setText(R.string.tips_network_parse_error);
			errorIndicatorLayout.setVisibility(View.VISIBLE);
			break;
		case EXCEPTION_TYPE_SERVER_NO_RESPONSE:
			errorMsgTextView.setText(R.string.tips_server_not_response_error);
			errorIndicatorLayout.setVisibility(View.VISIBLE);
			break;
		case EXCEPTION_TYPE_EMPTY_RESULT:
			errorMsgTextView.setText(R.string.tips_no_related_information);
			errorIndicatorLayout.setVisibility(View.VISIBLE);
			break;
		default:
			break;
		}
	}

	@Override
	public View getErrorIndicatorLayout() {
		return null;
	}

	@Override
	public TextView getErrorMsgTextView() {
		return null;
	}

	@Override
	public View getLoadingIndicatorView() {
		return null;
	}

	@Override
	public boolean needShowLoadingIndicator() {
		return true;
	}

	@Override
	public void onLoadingIndicatorShow(StatusEnum statusEnum) {
		View view = getLoadingIndicatorView();
		if (view != null) {
			view.setVisibility(View.VISIBLE);
		}

	}

	@Override
	public final void onLoadingIndicatorHide() {
		View view = getLoadingIndicatorView();
		if (view != null) {
			view.setVisibility(View.GONE);
		}

	}
}
